package com.hsource.doctorcenter.controller;

import com.hsource.common.utils.CommonResp;
import com.hsource.common.utils.DateUtil;
import com.hsource.common.utils.resultUtils.controller.BaseController;
import com.hsource.doctorcenter.domain.healthRecords.HealthRecords;
import com.hsource.doctorcenter.service.healthRecords.IHealthRecordsService;
import com.hsource.doctorcenter.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/9/25 10:05
 * @describe 健康档案
 */
@Controller
@RequestMapping("/healthRecords")
public class HealthRecordsController extends BaseController {
    @Autowired
    private IHealthRecordsService healthRecordsService;

    /**
     * 获取健康档案
     * @param userId
     * @return
     */
    @PostMapping("/getHealthRecords")
    @ResponseBody
    public CommonResp healthRecordsList(String userId){
        HealthRecords healthRecords=new HealthRecords();
        healthRecords.setUserId(userId);
        List<HealthRecords> healthRecordsList = healthRecordsService.selectHealthRecordsListByUserId(healthRecords);
        healthRecordsList.stream().forEach(a->{
           a.setBirthdayDate(DateUtil.getStrFromDate( a.getBirthday(),"yyyy-MM-dd"));
        });
        Map<String,Object> map=new HashMap<>();
        map.put("healthRecords",healthRecordsList);
        return success(map);
    }
    /**
     * 保存健康档案
     * @param healthRecords
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public CommonResp saveHealthRecords(HealthRecords healthRecords) throws Exception {

        HealthRecords hr=new HealthRecords();
        hr.setUserId(healthRecords.getUserId());
        List<HealthRecords> healthRecordsList = healthRecordsService.selectHealthRecordsList(hr);
        if(StringUtils.isNotNull(healthRecordsList)&&healthRecordsList.stream().count()>=8){
            return failed("健康档案最多创建8位");
        }
        healthRecordsService.insertHealthRecords(healthRecords);
        Map<String,Object> map=new HashMap<>();
        map.put("healthRecordsId",healthRecords.getId());
        return success(map);
    }
    /**
     * 保存健康档案0
     * @param healthRecords
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public CommonResp updateHealthRecords(HealthRecords healthRecords) throws Exception {
        healthRecords.setBirthday(DateUtil.getDateFromStr(healthRecords.getBirthdayDate()+" 00:00:00",""));
        healthRecordsService.updateHealthRecords(healthRecords);
        Map<String,Object> map=new HashMap<>();
        map.put("healthRecordsId",healthRecords.getId());
        return success(map);
    }
    /**
     * 删除健康档案
     * @param id 主键
     * @return
     */
    @PostMapping("/del")
    @ResponseBody
    public CommonResp delHealthRecords(String id){
        healthRecordsService.deleteHealthRecordsById(id);
        return success();
    }
}
